<template>
  <div>
    <el-card>
      <div class="header">
        <el-button type="primary" @click="handleAdd">创建部门</el-button>
      </div>
      <el-table :data="tableData" style="width: 100%; margin-top: 30px">
        <el-table-column prop="name" label="部门名称" align="center">
        </el-table-column>
        <el-table-column prop="userName" label="负责人" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.tenantId!=='0'" size="mini" type="primary" @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button v-if="scope.row.tenantId!=='0'" size="mini" type="danger" @click="del(scope.row.id)"
            >删除
            </el-button>
            <span v-else>系统默认选项，无法操作</span>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
      <create ref="creat" @Ok="handleInit"></create>
      <edit ref="edit" @Ok="handleInit"></edit>
    </el-card>
  </div>
</template>
<script>
  import {list, remove} from "@/api/company";
  import Create from "./components/DepartmentCreat";
  import edit from "./components/DepartmentEdit";

  export default {
    name: "Department",
    components: {
      Create,
      edit,
    },
    data() {
      return {
        tableData: [],
        page: 1,
        size: 10,
        total: 1,
      };
    },
    mounted() {
      this.handleInit();
    },
    methods: {
      //列表
      handleInit() {
        let form = {
          page: this.page,
          size: this.size,
        };
        list(form).then((res) => {
          console.log(res);
          this.tableData = res.result.records;
          this.total = Number(res.result.total);
        });
        // list(form).then((res) => {});
      },
      //创建
      handleAdd() {
        this.$refs["creat"].show();
      },
      //编辑
      handleEdit(e) {
        this.$refs["edit"].show(e);
      },
      //删除
      del(id) {
        this.id = id;
        console.log(id);
        let form = {
          id: this.id,
        };
        remove(form).then((res) => {
          if (res.status == 200) {
            this.$message.success("删除成功");
            this.handleInit();
          } else {
            this.$message.error("删除失败");
            this.handleInit();
          }
        });
      },
      //分页
      handleCurrentChange(val) {
        console.log(val);
        this.page = val;
        this.handleInit();
      },
    },
  };
</script>

<style scoped>
  .header {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
</style>
